<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="画像" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :action="action"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :file-list="form.files"
          accept="image/jpeg,image/gif,image/png,image/bmp"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/nav'
  import { baseURL, fileServerURL} from '@/config/settings'

  export default {
    name: 'NavEdit',
    data() {
      return {
        form: {
          id: 0,
          title: '',
          url:''
        },
        formLabelWidth: '80px',
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        action:'',
        title: '',
        imageUrl: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.action = baseURL + '/erp/file/upload'
    },
    methods: {
      showEdit(row) {
        this.imageUrl = ''
        if (!row || !row.id) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          if(this.form.url){
            this.imageUrl = fileServerURL + this.form.url
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let msg = ''
            if (this.form.id > 0) {
              await doEdit(this.form)
              msg = '数据更新成功'
            } else {
              await doAdd(this.form)
              this.imageUrl = ''
              this.title = ''
              msg = '数据追加成功'
            }
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      handleAvatarSuccess(res, file) {
        this.form.url = res.filePath
        this.imageUrl = URL.createObjectURL(file.raw)
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isGIF = file.type === 'image/gif'
        const isPNG = file.type === 'image/png'
        const isBMP = file.type === 'image/bmp'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG && !isGIF && !isPNG && !isBMP) {
          this.common.errorTip('上传图片必须是JPG/GIF/PNG/BMP 格式!')
        }
        if (!isLt2M) {
          this.common.errorTip('上传图片大小不能超过 2MB!')
        }
        return (isJPG || isBMP || isGIF || isPNG) && isLt2M
      },
    },
  }
</script>
